package com.lyl.servicelive.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyl.exception.ZxjyException;
import com.lyl.servicelive.entity.Course;
import com.lyl.servicelive.entity.CourseConfig;
import com.lyl.servicelive.entity.CourseDescription;
import com.lyl.servicelive.entity.CourseGoods;
import com.lyl.servicelive.entity.vo.LiveCourseConfigVo;
import com.lyl.servicelive.entity.vo.LiveCourseGoodsVo;
import com.lyl.servicelive.mapper.CourseConfigMapper;
import com.lyl.servicelive.mtcloud.CommonResult;
import com.lyl.servicelive.mtcloud.MTCloud;
import com.lyl.servicelive.service.CourseConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyl.servicelive.service.CourseGoodsService;
import com.lyl.servicelive.service.CourseService;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 直播课程配置表 服务实现类
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-07-05
 */
@Service
public class CourseConfigServiceImpl extends ServiceImpl<CourseConfigMapper, CourseConfig> implements CourseConfigService {



    @Autowired
    private CourseGoodsService goodsService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private MTCloud mtCloudClient;


    @Override
    public LiveCourseConfigVo getCourseConfig(Long id) {
        LiveCourseConfigVo liveCourseConfigVo = new LiveCourseConfigVo();
        CourseConfig liveCourseConfig = this.getByLiveCourseId(id);
        if(null != liveCourseConfig) {
            List<CourseGoods> liveCourseGoodsList = goodsService.findByLiveCourseId(id);
            BeanUtils.copyProperties(liveCourseConfig, liveCourseConfigVo);
            liveCourseConfigVo.setLiveCourseGoodsList(liveCourseGoodsList);
        }
        return liveCourseConfigVo;
    }

    @Override
    public CourseConfig getByLiveCourseId(Long id) {
        QueryWrapper<CourseConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("live_course_id", id);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean updateConfig(LiveCourseConfigVo liveCourseConfigVo) {

        CourseConfig liveCourseConfigUpt = new CourseConfig();
        BeanUtils.copyProperties(liveCourseConfigVo, liveCourseConfigUpt);

        if(null == liveCourseConfigVo.getId()) {
            System.out.println("新增");
            this.save(liveCourseConfigUpt);
        } else {
            System.out.println("修改");
            this.updateById(liveCourseConfigUpt);
        }
        // 批量移除之前的商品
        QueryWrapper<CourseGoods> removes= new QueryWrapper<>();
        removes.eq("live_course_id",liveCourseConfigVo.getLiveCourseId());
        goodsService.remove(removes);


        if(!CollectionUtils.isEmpty(liveCourseConfigVo.getLiveCourseGoodsList())) {
           // 新增新商品
            goodsService.saveBatch(liveCourseConfigVo.getLiveCourseGoodsList());
        }
       return this.updateLifeConfig(liveCourseConfigVo);

    }
    /**
     * 上传直播配置
     * @param liveCourseConfigVo
     */
    @SneakyThrows
    public boolean updateLifeConfig(LiveCourseConfigVo liveCourseConfigVo) {
        Course liveCourse = courseService.getById(liveCourseConfigVo.getLiveCourseId());

        //参数设置
        HashMap<Object,Object> options = new HashMap<Object, Object>();
        //是否开启弹幕
        options.put("barrage", 1);
        //界面模式
        options.put("pageViewMode", liveCourseConfigVo.getPageViewMode());
        //观看人数开关
        JSONObject number = new JSONObject();
        number.put("enable", liveCourseConfigVo.getNumberEnable());
        options.put("number", number.toJSONString());
//        //商城开关

        JSONObject store = new JSONObject();
        store.put("enable", liveCourseConfigVo.getStoreEnable());
        store.put("type", 1);//商城列表
        options.put("store", store.toJSONString());
        System.out.println("商城开关:"+store );
        //商城列表
        List<CourseGoods> liveCourseGoodsList = liveCourseConfigVo.getLiveCourseGoodsList();
        if(!CollectionUtils.isEmpty(liveCourseGoodsList)) {
            List<LiveCourseGoodsVo> liveCourseGoodsViewList = new ArrayList<>();
            for(CourseGoods liveCourseGoods : liveCourseGoodsList) {
                LiveCourseGoodsVo liveCourseGoodsView = new LiveCourseGoodsVo();
                BeanUtils.copyProperties(liveCourseGoods, liveCourseGoodsView);

                System.out.println("liveCourseGoodsView商品列表:"+ liveCourseGoodsView);
                System.out.println("liveCourseGoods:"+ liveCourseGoodsView);
                // 市场价是原价的1.3倍
                liveCourseGoodsView.setOriginalPrice(liveCourseGoodsView.getPrice().multiply(new BigDecimal(1.3)));
                liveCourseGoodsViewList.add(liveCourseGoodsView);
            }
            JSONObject goodsListEdit = new JSONObject();

            goodsListEdit.put("status", "0");
            options.put("goodsListEdit ", goodsListEdit.toJSONString());
            options.put("goodsList", JSON.toJSONString(liveCourseGoodsViewList));
        }

        System.out.println("修改配置:options-->"+options);

        String res = mtCloudClient.courseUpdateLifeConfig(liveCourse.getCourseId().toString(),
                options);
        System.out.println("修改配置:"+ res+"\n"+liveCourse.getCourseId());

        CommonResult<JSONObject> commonResult = JSON.parseObject(res, CommonResult.class);
        if(Integer.parseInt(commonResult.getCode()) != MTCloud.CODE_SUCCESS) {
            throw new ZxjyException(20001,"修改配置信息失败");
        }

        return  true;
    }
}
